<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px">
            <el-form-item label="模版名称" prop="templateName">
                <el-input v-model="queryParams.templateName" placeholder="请输入模版名称" clearable />
            </el-form-item>
            <el-form-item label="上传人" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入上传人" clearable />
            </el-form-item>
            <el-form-item label="上传日期" prop="time">
                <el-date-picker v-model="queryParams.time" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>


        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5" v-if="permission">
                <el-button type="primary" @click="handleUpload">
                    <svg-icon class-name="upload-icon" icon-class="upload" /><span style="margin-left:10px">上传</span>
                </el-button>
            </el-col>
            <el-col :span="1.5" v-if="permission">
                <el-button type="primary" icon="Delete" :disabled="multiple" @click="handleDelete"
                    >批量删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" icon="Download" :disabled="multiple" @click="handleDownloads">批量下载</el-button>
            </el-col>
        </el-row>

        <el-table ref="tableRef" v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange" row-key="id">
            <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
            <el-table-column label="模板名称" align="center" prop="templateName" width="255" show-overflow-tooltip />
            <el-table-column label="模版描述" align="center" prop="templateDesc" width="255" show-overflow-tooltip/>
            <el-table-column label="上传人" align="center" prop="userName" />
            <el-table-column label="上传日期" align="center" prop="createTime" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" @click="handleDown(scope.row)">下载</el-button>
                    <el-button type="text" @click="handleDelete(scope.row)" v-if="permission">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
                @pagination="getList" />

        <el-dialog title="图件模板上传" v-model="openFile" width="500px" top="200px" append-to-body>
            <el-form ref="uploadRef" label-width="120px" :model="form" :rules="rules">
                <el-row style="margin: 20px 0;">
                    <el-col>
                        <el-form-item label="模版上传" prop="attachId">
                            <file-upload :action="uploadFileUrl" :limit="1" ref="fileUpload" v-model="form.fileList"
                                :fileType='["ult"]' @uploadFile="uploadFile" @click="delFile"/>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="模版名称" prop="templateName">
                            <el-input v-model="form.templateName" placeholder="请输入模版名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="描述">
                            <el-input type="textarea" maxlength="100" show-word-limit
                                :autosize="{ minRows: 3, maxRows: 5 }" placeholder="请输入描述" v-model="form.templateDesc"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="fileSubmitForm">保 存</el-button>
                    <el-button @click="openFile = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="imgTemplateManage">
import { getAllList, saveReportTemplate, delReportTemplate, getPermission } from "@/api/iconModle/imgTemplateManage.js"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const ids = ref();
const attachIds = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const permission = ref(false);
const total = ref(0);
const openFile = ref(false);
const uploadFileUrl = ref(
    import.meta.env.VITE_APP_PORTAL_API + "/attachment/uploadFile"
); // 上传的图片服务器地址
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        templateName: undefined,
        userName: undefined,
        time: undefined,
    },
    form: {
        fileList: [],
        attachId: '',
        templateName:undefined,
        templateDesc:undefined,
    },
    rules: {
        attachId: [{ required: true, message: "模板不能为空", trigger: "blur" }],
        templateName: [{ required: true, message: "模板名称不能为空", trigger: "blur" }],
    }
})
const { queryParams, form, rules } = toRefs(data)


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList({ ...queryParams.value, startTime: queryParams.value.time ? queryParams.value.time[0] : undefined, endTime: queryParams.value.time ? queryParams.value.time[1] : undefined }).then(response => {
        dataList.value = response.data.rows ? response.data.rows : [];
        total.value = response.data.total;
        loading.value = false;
        proxy.$refs["tableRef"].clearSelection();
    });
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    attachIds.value = selection.map(item => item.attachId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
    console.log(attachIds.value,'lallalal');
}

/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.id || ids.value.join(',');
    proxy.$modal.confirm('是否确认删除选中的数据项么').then(function () {
        return delReportTemplate(id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

function uploadFile() {
    form.value.attachId = JSON.parse(JSON.stringify(proxy.$refs["fileUpload"].fileList))[0].attachId;
}

function delFile() {
    form.value.attachId = '';
}

// 上传 
function handleUpload() {
    openFile.value = true;
    form.value = {
        fileList: [],
        attachId: '',
        templateName:undefined,
        templateDesc:undefined,
    },
        proxy.resetForm("uploadRef");
}

function fileSubmitForm() {
    proxy.$refs["uploadRef"].validate(valid => {
        if (valid) {
            saveReportTemplate({...form.value}).then(res => {
                proxy.$modal.msgSuccess("上传成功");
                openFile.value = false;
                getList();
            })
        }
    })
}

// 单个下载文件
function handleDown(row) {
    var query = {
        attachId: row.attachId,
    }
    location.href = import.meta.env.VITE_APP_PORTAL_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(query.attachId);
}

/** 批量下载操作 */
function handleDownloads() {
	proxy.download( import.meta.env.VITE_APP_PORTAL_API +"/attachment/zipDownload/" + attachIds.value.join(","), {}, '图件模版.zip');
};

function getPermissionAdmin() {
    getPermission().then(res => {
        permission.value = res.data;
    })
}
getPermissionAdmin();
getList();
</script>

<style lang="scss" scoped></style>